package com.threeti.mecool.core.domain.model.project;

import com.threeti.mecool.core.domain.model.basicdata.biz.ChannelSync;
import com.threeti.mecool.framework.domain.support.DefaultDomainEntity;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目门店(指向某个 系统通路，别名也称为终端)
 */
@Entity
@Table(name = "DM_PROJECT_STORE",
        uniqueConstraints = @UniqueConstraint(columnNames = {"PROJECT", "CHANNEL_CODE"}))
public class ProjectStore extends DefaultDomainEntity {

    @ManyToOne(optional = false)
    private Project project;

    //  @Embedded
//  private Channel channel;
    @Column(name = "CHANNEL_CODE")
    private String channelCode;//only for unique constraints purpose

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private ChannelSync channel;

    @ManyToMany(mappedBy = "assignedStores")
//    @JoinTable(name = "DM_PROJECT_SALESPROMOTER_STORE",
//            joinColumns = @JoinColumn(name = "PROJSTORE_ID"),
//            inverseJoinColumns = @JoinColumn(name = "PROJSALES_ID"))
    private List<ProjectSalesPromoter> projectSalesPromoters = new ArrayList<ProjectSalesPromoter>();

    @OneToOne(mappedBy = "projectStore", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private ProjectStoreSellingTarget sellingTarget;

    private Integer requiredSalesNumber;

    public ProjectStore(Project project, ChannelSync channel) {
        super();
        this.project = project;
        this.channel = channel;
        this.channelCode = channel.getCode();
    }

    /**
     * 取得门店所在项目
     */
    public Project getProject() {
        return project;
    }

    /**
     * 取得门店的通路信息
     */
    public ChannelSync getChannel() {
        return channel;
    }

    public String getChannelCode() {
        return channelCode;
    }

    public void setChannelCode(String channelCode) {
        this.channelCode = channelCode;
    }

    public List<ProjectSalesPromoter> getProjectSalesPromoters() {
        return projectSalesPromoters;
    }

    public void setProjectSalesPromoters(List<ProjectSalesPromoter> projectSalesPromoters) {
        this.projectSalesPromoters = projectSalesPromoters;
    }

    public Integer getRequiredSalesNumber() {
        return requiredSalesNumber == null ? -1 : requiredSalesNumber;
    }

    public void setRequiredSalesNumber(Integer requiredSalesNumber) {
        this.requiredSalesNumber = requiredSalesNumber;
    }

    public ProjectStoreSellingTarget getSellingTarget() {
        return sellingTarget;
    }

    public void setSellingTarget(ProjectStoreSellingTarget sellingTarget) {
        this.sellingTarget = sellingTarget;
    }

    ProjectStore() {
        super();
    }

    @Override
    public String toString() {
        return "ProjectStore [channel=" + channel + ", id=" + getId() + "]";
    }


}
